{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python385jvsc74a57bd0d9f66181372224249f2418c684328308052feb45d745ec7e6ec240f312e26c3b",
   "display_name": "Python 3.8.5 64-bit ('base': conda)"
  },
  "metadata": {
   "interpreter": {
    "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "numpy array"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Requirement already satisfied: numpy in /Users/rickyxing/anaconda3/lib/python3.8/site-packages (1.19.2)\n"
     ]
    }
   ],
   "source": [
    "#install module\n",
    "!pip install numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "(array([[0, 1, 2],\n",
       "        [3, 4, 5],\n",
       "        [6, 7, 8]]),\n",
       " numpy.ndarray,\n",
       " 2,\n",
       " array([[0, 3, 6],\n",
       "        [1, 4, 7],\n",
       "        [2, 5, 8]]),\n",
       " array([[0, 3, 6],\n",
       "        [1, 4, 7],\n",
       "        [2, 5, 8]]))"
      ]
     },
     "metadata": {},
     "execution_count": 42
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.arange(9).reshape(3,3)\n",
    "arr, type(arr),  arr.ndim , arr.T , arr.transpose(1,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "(array([[2],\n",
       "        [5]]),\n",
       " array([1, 4]),\n",
       " array([[0],\n",
       "        [3],\n",
       "        [6]]))"
      ]
     },
     "metadata": {},
     "execution_count": 30
    }
   ],
   "source": [
    "# 切片\n",
    "arr[:2,2:] ,  arr[:2,1] , arr[:,:1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[0, 3, 6],\n",
       "       [1, 4, 7],\n",
       "       [2, 5, 8]])"
      ]
     },
     "metadata": {},
     "execution_count": 32
    }
   ],
   "source": [
    "# 转置\n",
    "arr.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "(array([[[ 0,  1,  2,  3],\n",
       "         [ 4,  5,  6,  7]],\n",
       " \n",
       "        [[ 8,  9, 10, 11],\n",
       "         [12, 13, 14, 15]]]),\n",
       " 3)"
      ]
     },
     "metadata": {},
     "execution_count": 28
    }
   ],
   "source": [
    "# 高阶数组\n",
    "arr2 = np.arange(16).reshape(2,2,4)\n",
    "arr2 , arr2.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3],\n",
       "        [ 8,  9, 10, 11]],\n",
       "\n",
       "       [[ 4,  5,  6,  7],\n",
       "        [12, 13, 14, 15]]])"
      ]
     },
     "metadata": {},
     "execution_count": 34
    }
   ],
   "source": [
    "# 根据轴编号，置换轴\n",
    "arr2.transpose((1,0,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]],\n",
       "\n",
       "       [[12, 13, 14, 15],\n",
       "        [16, 17, 18, 19],\n",
       "        [20, 21, 22, 23]]])"
      ]
     },
     "metadata": {},
     "execution_count": 39
    }
   ],
   "source": [
    "arr3 = np.arange(24).reshape(2,3,4)\n",
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3],\n",
       "        [12, 13, 14, 15]],\n",
       "\n",
       "       [[ 4,  5,  6,  7],\n",
       "        [16, 17, 18, 19]],\n",
       "\n",
       "       [[ 8,  9, 10, 11],\n",
       "        [20, 21, 22, 23]]])"
      ]
     },
     "metadata": {},
     "execution_count": 43
    }
   ],
   "source": [
    "# 对轴x,y进行互换，轴z不变。\n",
    "arr3.transpose((1,0,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "(array([[[ 0,  1,  2,  3],\n",
       "         [ 4,  5,  6,  7],\n",
       "         [ 8,  9, 10, 11]],\n",
       " \n",
       "        [[12, 13, 14, 15],\n",
       "         [16, 17, 18, 19],\n",
       "         [20, 21, 22, 23]]]),\n",
       " array([[[ 0, 12],\n",
       "         [ 4, 16],\n",
       "         [ 8, 20]],\n",
       " \n",
       "        [[ 1, 13],\n",
       "         [ 5, 17],\n",
       "         [ 9, 21]],\n",
       " \n",
       "        [[ 2, 14],\n",
       "         [ 6, 18],\n",
       "         [10, 22]],\n",
       " \n",
       "        [[ 3, 15],\n",
       "         [ 7, 19],\n",
       "         [11, 23]]]))"
      ]
     },
     "metadata": {},
     "execution_count": 48
    }
   ],
   "source": [
    "arr3,arr3.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2],\n",
       "        [ 3,  4,  5]],\n",
       "\n",
       "       [[ 6,  7,  8],\n",
       "        [ 9, 10, 11]]])"
      ]
     },
     "metadata": {},
     "execution_count": 56
    }
   ],
   "source": [
    "arr4 = np.arange(12).reshape(2,2,3)\n",
    "arr4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2],\n",
       "        [ 6,  7,  8]],\n",
       "\n",
       "       [[ 3,  4,  5],\n",
       "        [ 9, 10, 11]]])"
      ]
     },
     "metadata": {},
     "execution_count": 54
    }
   ],
   "source": [
    "arr4.transpose(1,0,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([ 2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "metadata": {},
     "execution_count": 59
    }
   ],
   "source": [
    "arr4[ arr4 > 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([1, 3, 5, 7, 9])"
      ]
     },
     "metadata": {},
     "execution_count": 63
    }
   ],
   "source": [
    "# 以2为步长生成数组\n",
    "data1 = np.arange(1,10,2)\n",
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[array([[1, 3, 5, 7, 9],\n",
       "        [1, 3, 5, 7, 9],\n",
       "        [1, 3, 5, 7, 9],\n",
       "        [1, 3, 5, 7, 9],\n",
       "        [1, 3, 5, 7, 9]]),\n",
       " array([[1, 1, 1, 1, 1],\n",
       "        [3, 3, 3, 3, 3],\n",
       "        [5, 5, 5, 5, 5],\n",
       "        [7, 7, 7, 7, 7],\n",
       "        [9, 9, 9, 9, 9]])]"
      ]
     },
     "metadata": {},
     "execution_count": 64
    }
   ],
   "source": [
    "# 将两个一维数组，合并为 二维数组\n",
    "data2 = np.meshgrid(data1,data1)\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "metadata": {},
     "execution_count": 65
    }
   ],
   "source": [
    "data3 = np.array([1,2,3,4,5])\n",
    "data3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "(15, 3.0)"
      ]
     },
     "metadata": {},
     "execution_count": 67
    }
   ],
   "source": [
    "data3.sum() , data3.mean() # mean为求平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "metadata": {},
     "execution_count": 68
    }
   ],
   "source": [
    "data4 = np.arange(12).reshape(3,4)\n",
    "data4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "(array([12, 15, 18, 21]),\n",
       " array([ 6, 22, 38]),\n",
       " array([12, 15, 18, 21]),\n",
       " array([ 6, 22, 38]))"
      ]
     },
     "metadata": {},
     "execution_count": 74
    }
   ],
   "source": [
    "data4.sum(axis=0) , data4.sum(axis=1) , data4.sum(0) , data4.sum(1) # axis=0 按0轴求各。0轴为竖向， axis=1按轴1求和，轴1为横向。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "(3, 4)"
      ]
     },
     "metadata": {},
     "execution_count": 72
    }
   ],
   "source": [
    "data4.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "(array([ 2,  3,  4,  5,  6,  7,  8,  9, 10, 11]),\n",
       " array([[False, False,  True,  True],\n",
       "        [ True,  True,  True,  True],\n",
       "        [ True,  True,  True,  True]]),\n",
       " 66,\n",
       " 10)"
      ]
     },
     "metadata": {},
     "execution_count": 85
    }
   ],
   "source": [
    "# 使用[]筛选出数据，并对结果数据求和； (data4 > 1)结果为布尔数组，sum为求个数\n",
    "data4[data4>1], (data4 > 1) , data4[data4 > 0].sum() , ( data4  > 1 ).sum()"
   ]
  }
 ]
}